Part Number Hot Search : 
39000 MUR7010 ST1000 RF1660 AM7307X 1D850 RF200 STPS30
Product Description
Full Text Search
 

To Download AN1504 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  AN1504/0302 1/9 AN1504 application note starting a pwm signal directly at high level using the st7 16-bit timer by microcontroller division applications introduction the 16-bit timer is a standard peripheral of the st7 microcontroller family. this peripheral can be used for a variety of purposes, including pulse length measurement of up to two input sig- nals (input capture feature) or generation of up to two output waveforms (output compare and pwm mode). this application note is about using the pwm mode of the standard 16 bit timer. it explains how to synchronize the pwm signal output. in other words, how to make sure it outputs a high state when the counter restarts after it has been stopped (for any reason) or simply when it starts at the beginning of the application. in some applications, like motor control, it may be es- sential to output the high level part of the signal duty cycle when the counter is started. 1
2/9 starting a pwm signal directly at high level using the st7 16-bit timer 1 16-bit timer pwm mode 1.1 description in pulse width modulation mode, the frequency of the signal is determined by the value in output compare 2 register (oc2r) and the pulse length by the value in the output compare 1 register (ocr1) or duty cycle value. the olvl2 bit selects the level to be applied to the output pin after a successful comparison between the counter and the oc2r register and the olvl1 bit selects the level to be applied on the output after a successful comparison between the counter and the oc1r register 1.2 normal behaviour figure 1 shows the normal behaviour of output compare 1 (ocmp1) pin when a pwm signal is output with olvl2=1 and olvl1=0. figure 1. pwm output when olvl2=1 and olvl1=0 when the counter reaches the oc2r register value, the value of olvl2 is applied on the ocmp1 pin (=1 in this case). when the counter reaches the value of the oc1r register, the value of olvl1 is applied on the ocmp1 pin (=0). the formulas needed to compute the values to be put in oc2r and oc1r registers are in the 16-bit timer chapter of the st7 da- tasheets. as the 16-bit timer is reset at fffc, the formulas are: ocir=((t*fcpu)/presc)-5 where: t= period of the signal ocmp1 ouput compare pin timer output ffffh oc1r 0000h fffch ttimer 65535 tmax = olvl1=0 olvl2= 1 free running counter value time time oc2r ocmp1 ouput compare pin timer output ffffh oc1r 0000h fffch ttimer 65535 tmax = olvl1=0 olvl2= 1 free running counter value time time oc2r 2
3/9 starting a pwm signal directly at high level using the st7 16-bit timer fcpu= cpu frequency presc= timer 16 presc figure 2 shows the waveforms we see on an oscilloscope if the pwm signal is initialized at 10khz with a 50% duty cycle (olvl2=1 and olvl1=0) and if a flag is set as soon as the counter is started. figure 2. ocmp1 waveform with 50% duty cycle we can see from this figure that we have to wait until the counter reaches the oc2r register value to get the first high state on the ocmp1 pin. so we have lost one pwm cycle before get- ting the first high level and in some applications, like energizing motor windings, this is not ac- ceptable. after a motor demagnetization phase, if the windings are not energized immediately when the pwm is started, the motor can stall. the purpose of the following sections is to ex- plain how to get a high level on ocmp1 pin immediately. to solve the problem, we have to handle two cases. 1. the pwm signal is high when the timer is between fffc (its reset value) and the oc1r register value. this means olvl1=0 and olvl2=1. 2. the pwm signal is high when the timer is between the oc1r register value and the oc2r register value. this means olvl1=1 and olvl2=0. timer 16 start output enabled flag pwm output
4/9 starting a pwm signal directly at high level using the st7 16-bit timer 2 first case: olvl2=1 and olvl1=0 in this case, to force a high state on the ocmp1 pin when the timer is started: C initialize the timer in pwm mode and set the pwm frequency with a 0% duty cycle C reset the timer (at fffc) C load the oc1r register with a value close to fffc (fffd for example) and configure olvl1=1 and olvl2=0. C start the timer. it immediately reaches the value of the oc1r register and ocmp1 pin goes into the state defined by the olvl1 bit, which is high. C then, write the correct duty cycle value in the oc1r register and the correct state in the olvl2 and olvl1 bits (olvl2=1 and olvl1=0). the following code gives an example of how to restart (or start) the 16-bit timer this way. this is for timer a already initialized in pwm mode at 10khz with the 16-bit timer clocked at 1mhz (tacr2=10011000, taoc2hr=$00 and taoc2lr=$5f for 10khz at 1mhz clock. this is how to calculate the values to be put in the registers: C oc2r represents the signal period. the frequency is 10khz, so the period is t=100s. f cpu is 8mhz and the timer prescaler is 8 because the timer is at 1mhz. oc2r=((t*fcpu)/presc)-5=((100.10-6*8.10+6)/8)-5=95 (005f in hexadecimal). C oc1r represents the pulse length, the duty cycle is 50% so the pulse length is 100s*50%=50s. so the value to be put in oc1r is: oc1r=((50.10-6*8.10+6)/8)-5=45 (002d in hexadecimal). note: this method can be applied when the counter is first started as the starting value is 0000. initialization: in this example the duty cycle is first set to 0% with olvl1=0 and olvl2=0 ld a,#%00000000; set olvl1=0 and olvl2=0 ld tacr1,a ld a,#%10011000; clock in/8=1mhz with 16 mhz quartz ld tacr2,a ld a,#$00 ld taoc2hr,a ;10khz frequency for the pwm signal (see formulas) ld a,#$5f ld taoc2lr,a synchronization ld a,#%00000001;set olvl1=1 and olvl2=0 ld tacr1,a ;load corresponding control register 1 ld a,#$ff ld taoc1hr,a ;fix compare 1 to fffd to force pwm high as soon as possible ld a,#$fd
5/9 starting a pwm signal directly at high level using the st7 16-bit timer ld taoc1lr,a clr taclr ;reset timer b to fffc bset padr,#2 ;set flag synchronisation of timer b .wait_ta_c2 ld a,tachr jrne wait_ta_c2;wait timer a=0000 to restore duty cycle ld a,#%00000100 ld tacr1,a ;set olvl1=0 and olvl2=1 ld a,#$00 ld taoc1hr,a ld a,#$2d ;restore duty cycle to 50% ld taoc1lr,a bres padr,#2 ;reset synchronisation flag figure 3 shows the waveforms seen on an oscilloscope for ocmp1 pin and the synchroniza- tion flag using the above software example when the 16-bit timer is started or restarted. figure 3. ocmp1 and synchronization flag : olvl2=1 and olvl1=0 we can see in figure 3 that the ocmp1 pin outputs a high state directly after starting the pwm signal. this avoids losing a pwm cycle in applications where an immediate high state is required. pwm output timer 16 start output enabled flag
6/9 starting a pwm signal directly at high level using the st7 16-bit timer 3 second case: and olvl1=1 and olvl2=0 figure 4. pwm output when olvl2=0 olvl1=1 in this case, the timer a is also initialized at 10khz with a 1mhz timer clock. we can see that when olvl2=0 and olvl1=1, to set the duty cycle value, the oc1r register has to be loaded with the complementary value= compare 2-(the normal value of compare 1 in the case 1 example). so for example, with a 10khz pwm signal with a 1mhz timer clock, we have oc2r register =005f (see formulas) and so for 20% duty cycle, the normal value of the oc1r register is: oc1r=((20.10-6*8.10+6)/8))-5=15 (000f in hexadecimal) the complementary value is then oc1r=005f-000f=0050 in hexadecimal. for the initialization phase, we need to set a 0% duty cycle and in this case, instead of config- uring olvl2=olvl1=0 we can simply put a higher value in the oc1r register than in the oc2r register. this following code example is for a 20% duty cycle signal. initialization: set up 10khz pwm signal with olvl2=0 and olvl1=1 and 0% duty cycle ld a,#%00000001; set olvl1=1 and olvl2=0 ld tacr1,a ld a,#%10011000;clock in/8: 1mhz with 16 mhz quartz ld tacr2,a ld a,#$00 ld a,taoc2hr ;compare 2 of timer a set 005f=100-5=95 ld a,#$5f ld taoc2lr,a ;frequency set to 10khz ocmp1 ouput compare pin timer output ffffh oc1r 0000h fffch ttimer 65535 tmax = olvl1=1 olvl2= 0 free running counter value time time oc2r ocmp1 ouput compare pin timer output ffffh oc1r 0000h fffch ttimer 65535 tmax = olvl1=1 olvl2= 0 free running counter value time time oc2r
7/9 starting a pwm signal directly at high level using the st7 16-bit timer ld a,#$00 ld taoc1hr,a ld a,#$60 ld taoc1lr,a ; set 0% duty cycle by compare 1 value > compare 2 value after this initialization, the pwm synchronization at start is done by putting a value into the compare register that is just higher than the timer restart value. synchronization: ld x,#$ff ;set compare 1 to fffd just after the timer reset value fffc ld taoc1hr,x ;to force the output pwm high just after restart ld a,#$fd ld taoc1lr,a add a,#$0f ;ld compare 2 with compare 1+20% duty (000f) ld y,a ld a,x adc a,#$00 ld taoc2hr,a ld taoc2lr,y clr taclr ;reset timer a to fffc bset padr,#2 ;set synchronization flag of timer a .wait_ta_c1 ld a,tachr jrne wait_ta_c1;wait timer a counter=0000 to restore compare 1 ld a,#$00 ld taoc1hr,a ld a,#$50 ld taoc1lr,a ;restore compare 1 to 20% (compare 2*(1-20%)) .wait_ta_c2 btjf tachr,#7,wait_ta_c2;wait for reset of timer a before restoring com- pare 2 value ld a,#$00 ld taoc2hr,a ld a,#$5f ;restore compare 2 to 10khz=100-5=95 (see formulas) ld taoc2lr,a bres padr,#2 ;reset synchronization flag of timer b
8/9 starting a pwm signal directly at high level using the st7 16-bit timer figure 5. ocmp1 and flag : timer restart between compare 1 and compare 2 we can see on this figure that the ocmp1 pin outputs a high state immediately after starting the pwm signal with the 16 bit timer. this will avoid wasting pwm cycle for applications where an immediate high state is needed. pwm output timer 16 start output enabled flag
9/9 starting a pwm signal directly at high level using the st7 16-bit timer the present note which is for guidance only aims at providing customers with information regarding their products in order for them to save time. as a result, stmicroelectronics shall not be held liable for any direct, indirect or consequential damages with respect to any claims arising from the content of such a note and/or the use made by customers of the information contained herein in connexion with their products. information furnished is believed to be accurate and reliable. however, stmicroelectronics assumes no responsibility for the co nsequences of use of such information nor for any infringement of patents or other rights of third parties which may result from its use. no license is granted by implication or otherwise under any patent or patent rights of stmicroelectronics. specifications mentioned in this publicati on are subject to change without notice. this publication supersedes and replaces all information previously supplied. stmicroelectronics prod ucts are not authorized for use as critical components in life support devices or systems without the express written approval of stmicroele ctronics. the st logo is a registered trademark of stmicroelectronics ? 2002 stmicroelectronics - all rights reserved. purchase of i 2 c components by stmicroelectronics conveys a license under the philips i 2 c patent. rights to use these components in an i 2 c system is granted provided that the system conforms to the i 2 c standard specification as defined by philips. stmicroelectronics group of companies australia - brazil - canada - china - finland - france - germany - hong kong - india - israel - italy - japan malaysia - malta - morocco - singapore - spain - sweden - switzerland - united kingdom - u.s.a. http://www.st.com


▲Up To Search▲   

 
Price & Availability of AN1504

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X